//
//  RocProgrammingView.m
//  RocRemoteControlProject
//
//  Created by 新致李照鹏 on 2018/12/18.
//  Copyright © 2018年 Roc. All rights reserved.
//

#import "RocProgrammingView.h"

@interface RocProgrammingView ()<CAAnimationDelegate>
{
    CGRect rect_title;
    CGRect rect_home;
    CGRect rect_switch;
    CGRect rect_go;
    CGRect rect_dance;
    CGRect rect_repeat;
    CGRect rect_back;
    CGRect rect_clear;
    CGRect rect_up;
    CGRect rect_down;
    CGRect rect_left;
    CGRect rect_right;
}
// 数据源
@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, strong) NSArray *buttonRects;
// 每一步数组
@property (nonatomic, strong) NSMutableArray *stepArray;
// 当前动画的stepbutton
@property (nonatomic, strong) UIButton *animationingBtn;

@end

@implementation RocProgrammingView

#pragma mark - 界面逻辑
/**
  跳舞：
  点击随机生成4-10步动作数组：如 1 前后右右前左 2左右右右后前。生成之后就发送给设备执行
 */
- (void)toDo_Dance
{
    // 清除
    [self toDo_Clear];
    
    // 随机生成
    int x =  4 + (arc4random() % 7);
    for (int i = 0; i<x; i++) {
        int index =  arc4random() % 4;
        RocProgrammingViewButtonType type;
        if (index == 0) {
            type = RocProgrammingViewButtonUp;
        }else if (index == 1){
            type = RocProgrammingViewButtonDown;
        }else if (index == 2){
            type = RocProgrammingViewButtonLeft;
        }else{
            type = RocProgrammingViewButtonRight;
        }
        [self toDo_AddNewWtihType:type];
    }
    
}
/**
  随机R：
  随机生成一个action，大部分是播报声音 random。
 */
- (void)toDo_Repeat
{
    
}
/**
 回退
 回删一步。删掉最新添加的一步，黑板的图标有个反向的回弹，cmdArr删掉最后一位。
 */
- (void)toDo_Back
{
    if (self.animationingBtn) {
        return;
    }
    
    if (self.stepArray && self.stepArray.count != 0) {
        NSInteger index = self.stepArray.count-1;
        UIButton *button = self.buttons[index];
        self.animationingBtn = button;
        [self hidden_animationWithButton:button];
    }
}
/**
 清除
 清空所有指令；黑板直接清空。cmdArr=[];按C，解锁回删、前后左右R跳舞按钮
 */
- (void)toDo_Clear
{
    [self.stepArray removeAllObjects];
    for (UIButton *button in self.buttons) {
        [button.layer removeAllAnimations];
        if (!button.hidden) {
            button.hidden = YES;
        }
    }
}

/**
  上下左右
  编程按钮点后，在黑板上按照划线的顺序添加一个相应的图标。效果：中心放大，0.6秒（0.3,0.2,0.1），从0%-110%-95%-100%回弹效果,具体可自调，有成熟的亦可用。
 */
- (void)toDo_AddNewWtihType:(RocProgrammingViewButtonType)type
{
    if (self.stepArray && self.stepArray.count < self.buttons.count) {
        NSInteger index = self.stepArray.count;
        [self setUpButtonWith:index Type:type];
        [self.stepArray addObject:[NSNumber numberWithInt:type]];
    }
}

/**
 GO
 点击后，发送motionStart，将黑板上（按照线的方向）编好的指令数组cmdArr[…]，发送到设备端解析执行；锁住回删、前后左右R跳舞按钮。
 设备接到指令开始运动，此时再按GO，交替发送motionStop|motionStart  控制设备 暂停|继续 运动。。
 设备走完需上传 motionOver 指令， APP接到后清空cmdArr；解锁回删、前后左右R跳舞按钮。提示“已经走完”，清空黑板。等待下一次编程输入。
 */
- (void)toDo_GO
{
    
}

// 锁住回删Clear前后左右R跳舞按钮


#pragma mark - animation

// 显示stepbutton
- (void)show_animationWithButton:(UIButton *)button
{
    // 设定为缩放
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    keyAnimation.values = @[
                            [NSNumber numberWithFloat:0.0],
                            [NSNumber numberWithFloat:1.1],
                            [NSNumber numberWithFloat:0.9],
                            [NSNumber numberWithFloat:1.0]];
    keyAnimation.keyTimes = @[
                              [NSNumber numberWithFloat:0.0],
                              [NSNumber numberWithFloat:0.4],
                              [NSNumber numberWithFloat:0.1],
                              [NSNumber numberWithFloat:0.1],
                              ];
    keyAnimation.duration = 0.6;
    [button.layer addAnimation:keyAnimation forKey:@"scale-layer-show"];
}

// 显示stepbutton
- (void)hidden_animationWithButton:(UIButton *)button
{
    // 设定为缩放
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    keyAnimation.values = @[
                            [NSNumber numberWithFloat:1.0],
                            [NSNumber numberWithFloat:0.9],
                            [NSNumber numberWithFloat:1.1],
                            [NSNumber numberWithFloat:0.0]];
    keyAnimation.keyTimes = @[
                              [NSNumber numberWithFloat:0.0],
                              [NSNumber numberWithFloat:0.1],
                              [NSNumber numberWithFloat:0.1],
                              [NSNumber numberWithFloat:0.4],
                              ];
    keyAnimation.duration = 0.6;
    keyAnimation.delegate = self;
    keyAnimation.removedOnCompletion = NO;
    keyAnimation.fillMode = kCAFillModeForwards;
    [button.layer addAnimation:keyAnimation forKey:@"scale-layer-hidden"];
}

#pragma mark -动画代理方法
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    self.animationingBtn.hidden = YES;
    [self.stepArray removeLastObject];
    [self.animationingBtn.layer removeAllAnimations];
    self.animationingBtn = nil;
}

#pragma mark - 视图创建
/**
  视图初始化
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        rect_title = KROCConver_Rect(925, 43, 370, 75);
        rect_home = KROCConver_Rect(69, 118, 176, 135);
        rect_switch = KROCConver_Rect(1981, 118, 170, 135);
        rect_go = KROCConver_Rect(1279, 1063, 281, 133);
        
        rect_dance = KROCConver_Rect(50, 414, 168, 162);
        rect_repeat = KROCConver_Rect(1997, 414, 169, 146);
        rect_back = KROCConver_Rect(472, 1010, 233, 225);
        rect_clear = KROCConver_Rect(765, 1010, 233, 225);
        
        rect_up = KROCConver_Rect(50, 662, 169, 146);
        rect_down = KROCConver_Rect(50, 926, 169, 146);
        rect_left = KROCConver_Rect(1997, 662, 169, 146);
        rect_right = KROCConver_Rect(1997, 926, 169, 146);
        
        CGFloat width = 234;
        CGFloat hight = 232;
        
        self.buttonRects = @[
                             [NSValue valueWithCGRect:KROCConver_Rect(608, 182, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((608+width), 182, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((608+width*2), 182, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((608+width*3), 182, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((608+width*4), 182, width, hight)],
                             
                             [NSValue valueWithCGRect:KROCConver_Rect(402, 458, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((402+width), 458, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((402+width*2), 458, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((402+width*3), 458, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((402+width*4), 458, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((402+width*5), 458, width, hight)],
        
                             [NSValue valueWithCGRect:KROCConver_Rect(428, 728, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((428+width), 728, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((428+width*2), 728, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((428+width*3), 728, width, hight)],
                             [NSValue valueWithCGRect:KROCConver_Rect((428+width*4), 728, width, hight)],
                             ];
        
        
                        
                        
        self.stepArray = [NSMutableArray array];
        
        
        [self buildBG];
        
        [self buildTitle];
        [self buildHome];
        [self buildSwitchModel];
        [self buildGo];
        
        [self buildDance];
        [self buildRepeat];
        [self buildBack];
        [self buildClear];
        
        [self buildUp];
        [self buildDown];
        [self buildLeft];
        [self buildRight];
        
        [self buildButtons];
    }
    return self;
}

/**
 *  背景图
 */
- (void)buildBG
{
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.image = [UIImage imageNamed:@"codeMode_BG.jpg"];
    [self addSubview:bgImageView];

}

/**
 *  标题图
 */
- (void)buildTitle
{
    //controlModeCut_03b
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect_title];
    imageView.contentMode = UIViewContentModeScaleToFill;
    imageView.image = [UIImage imageNamed:@"controlModeCut_03b.png"];
    [self addSubview:imageView];
}

/**
 *  主页图
 */
- (void)buildHome
{
    //controlModeCut_06
    UIImage *image = [UIImage imageNamed:@"controlModeCut_06.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_home];
    [button setImage:image forState:UIControlStateNormal];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionHome) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionHome
{
    if (self.mydelegate && [self.mydelegate respondsToSelector:@selector(RocProgrammingView:ClickButton:)]) {
        [self.mydelegate RocProgrammingView:self ClickButton:RocProgrammingViewButtonHome];
    }
}
/**
 *  切换图
 */
- (void)buildSwitchModel
{
    //controlModeCut_08b
    UIImage *image = [UIImage imageNamed:@"controlModeCut_08b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_switch];
    [button setImage:image forState:UIControlStateNormal];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionSwitch) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionSwitch
{
    if (self.mydelegate && [self.mydelegate respondsToSelector:@selector(RocProgrammingView:ClickButton:)]) {
        [self.mydelegate RocProgrammingView:self ClickButton:RocProgrammingViewButtonSwitch];
    }
}

/**
 *  开始图
 */
- (void)buildGo
{
    //codeModeCut_46
    UIImage *image = [UIImage imageNamed:@"codeModeCut_46.png"];
    UIImage *imageH = [UIImage imageNamed:@"codeModeCut_46b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_go];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionGo) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionGo
{
    if (self.mydelegate && [self.mydelegate respondsToSelector:@selector(RocProgrammingView:ClickButton:)]) {
        [self.mydelegate RocProgrammingView:self ClickButton:RocProgrammingViewButtonGO];
    }
}

/**
 *  跳舞
 */
- (void)buildDance
{
    //codeModeCut_19 codeModeCut_19b
    UIImage *image = [UIImage imageNamed:@"codeModeCut_19.png"];
    UIImage *imageH = [UIImage imageNamed:@"codeModeCut_19b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_dance];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionDance) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionDance
{
    [self toDo_Dance];
}

/**
 *  重置
 */
- (void)buildRepeat
{
    //codeModeCut_22 codeModeCut_22b
    UIImage *image = [UIImage imageNamed:@"codeModeCut_22.png"];
    UIImage *imageH = [UIImage imageNamed:@"codeModeCut_22b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_repeat];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionRepeat) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionRepeat
{
    
}

/**
 *  回退
 */
- (void)buildBack
{
    //controlModeCut_46 controlModeCut_46b
    UIImage *image = [UIImage imageNamed:@"controlModeCut_46.png"];
    UIImage *imageH = [UIImage imageNamed:@"controlModeCut_46b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_back];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionBack) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionBack
{
    [self toDo_Back];
}

/**
 * 清除
 */
- (void)buildClear
{
    //controlModeCut_49 controlModeCut_49b
    UIImage *image = [UIImage imageNamed:@"controlModeCut_49.png"];
    UIImage *imageH = [UIImage imageNamed:@"controlModeCut_49b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_clear];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionClear) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionClear
{
    [self toDo_Clear];
}
/**
 *  上
 */
- (void)buildUp
{
    //codeModeCut_31 codeModeCut_31b
    UIImage *image = [UIImage imageNamed:@"codeModeCut_31.png"];
    UIImage *imageH = [UIImage imageNamed:@"codeModeCut_31b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_up];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionUp) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionUp
{
    [self toDo_AddNewWtihType:RocProgrammingViewButtonUp];
}

/**
 *  下
 */
- (void)buildDown
{
    //codeModeCut_44 codeModeCut_44b
    UIImage *image = [UIImage imageNamed:@"codeModeCut_44.png"];
    UIImage *imageH = [UIImage imageNamed:@"codeModeCut_44b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_down];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionDown) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionDown
{
    [self toDo_AddNewWtihType:RocProgrammingViewButtonDown];
}

/**
 *  左
 */
- (void)buildLeft
{
    //codeModeCut_34 codeModeCut_34b
    UIImage *image = [UIImage imageNamed:@"codeModeCut_34.png"];
    UIImage *imageH = [UIImage imageNamed:@"codeModeCut_34b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_left];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionLeft) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionLeft
{
    [self toDo_AddNewWtihType:RocProgrammingViewButtonLeft];
}

/**
 * 右
 */
- (void)buildRight
{
    //codeModeCut_42 codeModeCut_42b
    UIImage *image = [UIImage imageNamed:@"codeModeCut_42.png"];
    UIImage *imageH = [UIImage imageNamed:@"codeModeCut_42b.png"];
    
    UIButton *button = [[UIButton alloc] initWithFrame:rect_right];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:imageH forState:UIControlStateHighlighted];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(buttonActionRight) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonActionRight
{
    [self toDo_AddNewWtihType:RocProgrammingViewButtonRight];
}

/**
 * 按钮数组
 */
- (void)buildButtons
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (int i = 0; i<self.buttonRects.count; i++) {
        CGRect rect = [self.buttonRects[i] CGRectValue];
        UIButton *button = [self getStepButtonWithRect:rect];
        [self addSubview:button];
        [mutableArray addObject:button];
    }
    self.buttons = mutableArray;
}
- (UIButton *)getStepButtonWithRect:(CGRect)rect
{
    //stepcodeModeCut_37 stepcodeModeCut_37b
    UIImage *image = [UIImage imageNamed:@"stepcodeModeCut_37.png"];
    UIImage *imageH = [UIImage imageNamed:@"stepcodeModeCut_06.png"];

    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(KROCConver_H(33), KROCConver_W(34), KROCConver_H(33), KROCConver_W(34))];
    [button setImage:imageH forState:UIControlStateNormal];
    button.hidden = YES;
    return button;
}

- (void)setUpButtonWith:(NSInteger)index Type:(RocProgrammingViewButtonType)type
{
    UIButton *button = self.buttons[index];
    if (button.hidden) {
        button.hidden = NO;
    }
    
    UIImage *image = [UIImage imageNamed:@"stepcodeModeCut_06.png"];
    UIImage *imageH = [UIImage imageNamed:@"stepcodeModeCut_06.png"];
    
    if (type == RocProgrammingViewButtonUp) {// stepcodeModeCut_06 stepcodeModeCut_06b
        image = [UIImage imageNamed:@"stepcodeModeCut_06.png"];
        imageH = [UIImage imageNamed:@"stepcodeModeCut_06b.png"];
    }else if (type == RocProgrammingViewButtonDown){// stepcodeModeCut_09 stepcodeModeCut_09b
        image = [UIImage imageNamed:@"stepcodeModeCut_09.png"];
        imageH = [UIImage imageNamed:@"stepcodeModeCut_09b.png"];
    }else if (type == RocProgrammingViewButtonLeft){// stepcodeModeCut_03 stepcodeModeCut_03b
        image = [UIImage imageNamed:@"stepcodeModeCut_03.png"];
        imageH = [UIImage imageNamed:@"stepcodeModeCut_03b.png"];
    }else if (type == RocProgrammingViewButtonRight){// stepcodeModeCut_25 stepcodeModeCut_25b
        image = [UIImage imageNamed:@"stepcodeModeCut_25.png"];
        imageH = [UIImage imageNamed:@"stepcodeModeCut_25b.png"];
    }else{
        return;
    }
    [button setImage:image forState:UIControlStateNormal];
    
    [self show_animationWithButton:button];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
